// 用于拼接字符串（自动插入换行）并替换到代码文件的类
function myString(dfText) {
    this.val = dfText || ''
    this.add = text => (this.val += (text || '') + '\r\n')
    this.writeTo = (strCode,tag) => {
        return strCode.replace(new RegExp(`\\@${tag},?`, 'g'), this.val.trimLeft())
    }
}


export const generatorIBll = (setting,temp,table)=>{
    let strCode = temp
    temp = null
    
    strCode = strCode.replace('@Namespace',setting.namespace.ibll)
    strCode = strCode.replace('@ProjectName',`${setting.project.f_Name}-${setting.moduleName}`)
    strCode = strCode.replace('@CompanyName',setting.company)
    strCode = strCode.replace('@CreateUserName',setting.userInfo.f_RealName)
    strCode = strCode.replace('@Datetime',setting.createTime)
    strCode = strCode.replace(/@Des/g,`${setting.des}`)

    // 主表处理
    let tableMethod = new myString()
    var deleteMethod = new myString()
    let saveMethod = new myString()
    if(table.type == 'main'){

        if(setting.qTables.length > 1){
            tableMethod.add(`        /// <summary>`)
            tableMethod.add(`        /// 获取@Des的列表数据`)
            tableMethod.add(`        /// <summary>`)
            tableMethod.add(`        /// <param name="queryParams">查询参数</param>`)
            tableMethod.add(`        /// <returns></returns>`)
            
            tableMethod.add(`        Task<IEnumerable<@MainTableEntity>> GetRelateList(@MainTableEntity queryParams);`)

            tableMethod.add(`        /// <summary>`)
            tableMethod.add(`        /// 获取@Des的分页数据`)
            tableMethod.add(`        /// <summary>`)
            tableMethod.add(`        /// <param name="pagination">分页参数</param>`)
            tableMethod.add(`        /// <param name="queryParams">查询参数</param>`)
            tableMethod.add(`        /// <returns></returns>`)
            
            tableMethod.add(`        Task<IEnumerable<@MainTableEntity>> GetRelatePageList(Pagination pagination, @MainTableEntity queryParams);`)
        }

        if(setting.dbTable.length > 1)  {
            // 保存方法
            saveMethod.add(`        /// <summary>`)
            saveMethod.add(`        /// 保存实体`)
            saveMethod.add(`        /// </summary>`)
            saveMethod.add(`        /// <param name="keyValue">主键</param>`)
            saveMethod.add(`        /// <param name="dto">数据集合</param>`)  
            saveMethod.add(`        /// <returns></returns>`)
            saveMethod.add(`        Task SaveAll(@PkType keyValue, ${window.$upFirst(setting.name)}Dto dto);`)
            

            // 删除方法
            deleteMethod.add(`        /// <summary>`)
            deleteMethod.add(`        /// 删除实体`)
            deleteMethod.add(`        /// </summary>`)   
            deleteMethod.add(`        /// <param name="keyValue">主键</param>`)  
            deleteMethod.add(`        /// <returns></returns>`)
            deleteMethod.add(`        Task DeleteAll(@PkType keyValue);`)
        } 
    }
    else{ // 子表处理
        if(table.relationType == '1'){
            tableMethod.add(`        /// <summary>`)
            tableMethod.add(`        /// 获取@Des的实体根据外键`)
            tableMethod.add(`        /// <summary>`)
            tableMethod.add(`        /// <param name="key">关联键</param>`)
            tableMethod.add(`        /// <returns></returns>`)
            tableMethod.add(`        Task<@MainTableEntity> GetRelateEntity(string key);`)
        }
        else{
            saveMethod.add(`        /// <summary>`)
            saveMethod.add(`        /// 保存子表数据`)
            saveMethod.add(`        /// <summary>`)
            saveMethod.add(`        /// <param name="key">外键值</param>`)
            saveMethod.add(`        /// <param name="list">数据集合</param>`)
            saveMethod.add(`        /// <returns></returns>`)
            saveMethod.add(`        Task SaveList(string key, IEnumerable<@MainTableEntity> list);`)
        }

        deleteMethod.add(`        /// <summary>`)
        deleteMethod.add(`        /// 删除@Des的实体根据外键`)
        deleteMethod.add(`        /// <summary>`)
        deleteMethod.add(`        /// <param name="key">关联键</param>`)
        deleteMethod.add(`        /// <returns></returns>`)
        deleteMethod.add(`        Task DeleteRelateEntity(string key);`)
    }

    strCode = tableMethod.writeTo(strCode,'TableMethod')
    tableMethod = null

    strCode = deleteMethod.writeTo(strCode,'DeleteMethod')
    deleteMethod = null

    strCode = saveMethod.writeTo(strCode,'SaveMethod')
    saveMethod = null


    let pkCsType = table.columns.find(t=>t.isPrimary).csType
    if(!['string'].includes(pkCsType)){
        pkCsType += '?'
    }


    strCode = strCode.replace(/@PkType/g,`${pkCsType}`)
    strCode = strCode.replace(/@ClassName/g,window.$upFirst(`I${table.className}BLL`))
    strCode = strCode.replace(/@MainTableEntity/g,window.$upFirst(table.className)+ 'Entity')
    return strCode;
}